package sk.vasicek.drinkmanager.activity;

import java.util.Locale;

import sk.vasicek.drinkmanager.R;
import sk.vasicek.drinkmanager.domain.DrinkPoint;
import sk.vasicek.drinkmanager.domain.Language;
import sk.vasicek.drinkmanager.service.BackgroundAdapter;
import sk.vasicek.drinkmanager.service.DrinkPlaceHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class DrinkMapDetailActivity extends Activity {
	private static String TAG = "DrinkMapDetailActivity";

	private String titleString;
	private String snippetString;

	private TextView title;
	private TextView snippet;

	private DrinkPlaceHelper drinkDatabaseHelper;

	private Integer id;
	private DrinkPoint drinkPoint;
	private RatingBar placeRating;

	private Button updateButton;

	private Button removeButton;
	protected ImageButton settingsButton;
	protected ImageButton mapButton;
	protected ImageButton placesButton;
	protected ImageButton drinksButton;

	SharedPreferences prefs;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		Configuration config = getBaseContext().getResources().getConfiguration();
		Locale locale = Language.getApplicationLanguage(this);
		Locale.setDefault(locale);
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
		setContentView(R.layout.drinkmap_detail);
		Log.d(TAG, "Started DrinkMapDetailActivity");

		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);

		drinkDatabaseHelper = new DrinkPlaceHelper(getApplicationContext());

		titleString = getIntent().getExtras().getString(DrinkPoint.POINT_TITLE);
		snippetString = getIntent().getExtras().getString(DrinkPoint.POINT_SNIPPET);
		id = getIntent().getExtras().getInt(DrinkPoint.POINT_ID, 0);

		this.title = (TextView) this.findViewById(R.id.title);
		this.snippet = (TextView) this.findViewById(R.id.snippet);
		this.placeRating = (RatingBar) this.findViewById(R.id.placeRating);
		this.updateButton = (Button) this.findViewById(R.id.updateButton);
		this.removeButton = (Button) this.findViewById(R.id.removeButton);

		this.title.setText(titleString);
		this.snippet.setText(snippetString);

		drinkPoint = drinkDatabaseHelper.getDrinkMapPointByID(id);
		if (drinkPoint != null) {
			this.title.setText(drinkPoint.getTitle());
			this.snippet.setText(drinkPoint.getSnippet());
			this.placeRating.setRating((float) drinkPoint.getRating());
		}

		updateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (DrinkMapDetailActivity.this.id != null) {
					if (drinkPoint != null) {
						DrinkMapDetailActivity.this.drinkPoint.setTitle(DrinkMapDetailActivity.this.title.getText() + "");
						DrinkMapDetailActivity.this.drinkPoint.setSnippet(DrinkMapDetailActivity.this.snippet.getText() + "");
						DrinkMapDetailActivity.this.drinkPoint.setRating((double) DrinkMapDetailActivity.this.placeRating.getRating());
						DrinkMapDetailActivity.this.drinkDatabaseHelper.updateDrinkMapPoint(DrinkMapDetailActivity.this.id, drinkPoint);
						Toast.makeText(DrinkMapDetailActivity.this.getApplicationContext(), getResources().getText(R.string.placeInfoUpdatedToast), Toast.LENGTH_SHORT).show();
					} else {
						String title = (String) DrinkMapDetailActivity.this.title.getText();
						String snippet = (String) DrinkMapDetailActivity.this.snippet.getText();
						double rating = (double) DrinkMapDetailActivity.this.placeRating.getRating();
						DrinkMapDetailActivity.this.drinkDatabaseHelper.updateDrinkMapPoint(DrinkMapDetailActivity.this.id, title, snippet, rating);
						Toast.makeText(DrinkMapDetailActivity.this.getApplicationContext(), getResources().getText(R.string.placeInfoUpdatedToast), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});

		removeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (DrinkMapDetailActivity.this.id != null) {
					AlertDialog.Builder builder = new AlertDialog.Builder(DrinkMapDetailActivity.this);
					builder.setMessage(getResources().getText(R.string.placeRemovePrompt)).setCancelable(true)
							.setPositiveButton(getResources().getText(R.string.yesAnswer), new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int id) {
									if (drinkPoint != null) {
										DrinkMapDetailActivity.this.drinkDatabaseHelper.removeDrinkMapPoint(drinkPoint);
										Toast.makeText(DrinkMapDetailActivity.this.getApplicationContext(), getResources().getText(R.string.placeRemoveToast), Toast.LENGTH_SHORT).show();
										DrinkMapDetailActivity.this.finish();
									} else {
										DrinkMapDetailActivity.this.drinkDatabaseHelper.removeDrinkMapPoint(DrinkMapDetailActivity.this.id);
										Toast.makeText(DrinkMapDetailActivity.this.getApplicationContext(), getResources().getText(R.string.placeRemoveToast), Toast.LENGTH_SHORT).show();
										DrinkMapDetailActivity.this.finish();
									}
								}
							}).setNegativeButton(getResources().getText(R.string.noAnswer), new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int id) {
									dialog.cancel();
								}
							});
					AlertDialog alert = builder.create();
					alert.show();

				}
			}
		});
		this.mapButton = (ImageButton) this.findViewById(R.id.map);
		mapButton.setVisibility(View.VISIBLE);
		this.mapButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();

				Intent intent = new Intent(getApplicationContext(), MapsOverlayActivity.class);
				startActivity(intent);
			}
		});
		this.placesButton = (ImageButton) this.findViewById(R.id.places);
		placesButton.setVisibility(View.VISIBLE);
		this.placesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), DrinkPlacesActivity.class);
				startActivity(intent);
			}
		});

		this.settingsButton = (ImageButton) this.findViewById(R.id.settings);
		settingsButton.setVisibility(View.VISIBLE);
		this.settingsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
				startActivity(intent);
			}
		});

		this.drinksButton = (ImageButton) this.findViewById(R.id.drinks);

		this.drinksButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), DrinkManagerListActivity.class);
				startActivity(intent);
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();

			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.optionmenu, menu);
		MenuItem settings = menu.findItem(R.id.settings);
		settings.setIntent(new Intent(this, SettingsActivity.class));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.exit:
			DrinkMapDetailActivity.this.finish();
			return true;
		case R.id.settings:
			Intent settingsIntent = item.getIntent();
			this.startActivityForResult(settingsIntent, 0);
			return true;
		case android.R.id.home:
			// Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(),
			// getResources().getText(R.string.alreadyToast),
			// Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(this, DrinkManagerListActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.drinks:
			Intent drinksIntent = new Intent(getApplicationContext(), DrinkManagerListActivity.class);
			this.startActivityForResult(drinksIntent, 0);
			return true;
		case R.id.places:
			Intent placesIntent = new Intent(getApplicationContext(), DrinkPlacesActivity.class);
			this.startActivityForResult(placesIntent, 0);
			return true;
		case R.id.custom:
			Intent customIntent = new Intent(this, CustomDrinksActivity.class);
			this.startActivityForResult(customIntent, 0);
//			Toast.makeText(getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.history:
			Intent historyIntent = new Intent(this, DrinkHistoryActivity.class);
			this.startActivityForResult(historyIntent, 0);
//			Toast.makeText(getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.graphs:
			Intent graphsIntent = new Intent(this, DrinkGraphActivity.class);
			this.startActivityForResult(graphsIntent, 0);
//			Toast.makeText(getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.back:
			Intent backIntent = new Intent(getApplicationContext(), BackgroundChooserActivity.class);
			this.startActivityForResult(backIntent, 0);
			return true;
		case R.id.map:
			Intent mapsIntent = new Intent(getApplicationContext(), MapsOverlayActivity.class);
			this.startActivityForResult(mapsIntent, 0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
